<?php

namespace app\admin\controller;


class Article extends BaseController {
    public function index() {
        $query = input('get.');
        if (!empty($query['start']) && !empty($query['end'])) {
            $condition['create_time'] = ['between', [$query['start'], $query['end']]];
        }
        if (!empty($query['status'])) {
            switch (intval($query['status'])) {
                case 1:
                    $condition['status'] = ['neq', config('code.status_delete')];
                    break;
                case 2:
                    $condition['status'] = config('code.status_unshow');
                    break;
                case 3:
                    $condition['status'] = config('code.status_show');
                    break;
            }
        }
        if (!empty($query['order'])) {
            switch (intval($query['order'])) {
                case 1:
                    $order = ['id' => 'desc'];
                    break;
                case 2:
                    $order = ['view_count' => 'desc'];
                    break;
                case 3:
                    $order = ['comment_count' => 'desc'];
                    break;
            }
        }
        if (!empty($query['keyword'])) {
            $condition['title|content'] = ['like', '%' . $query['keyword'] . '%'];
        }
        $this->getPageAndSize($query);
        $result = model('Article')->getArticleList($condition, $this->from, $this->size, !empty($order) ? $order : []);
        return show(0, 'get articlelist success', $result);
    }

    public function save() {
        $data = input('post.');
        if (!empty($data['id'])) {
            $id = model('Article')->edit($data, $data['id']);
        } else {
            $id = model('Article')->add($data);
        }
        return show(config('code.success'), '添加文章成功', ['id' => $id]);
    }

    public function update($id) {
        $data = input('put.');
        $type = input('get.type');
        if ($type == 1) {
            $result = model('Article')->where(['id' => $id])->update(['status' => $data['status']]);
            if (!$result) {
                return show(config('code.fail'), '修改状态失败');
            }
        }
        if ($type == 2) {
            $count = model('Article')->where('is_banner', 'eq', 1)->count();
            if ($count < config('setting.banner_count') || $data['is_banner'] == 0) {
                $result = model('Article')->where(['id' => $id])->update(['is_banner' => $data['is_banner']]);
                if (!$result) {
                    return show(config('code.fail'), '修改banner失败');
                }
            } else {
                return show(config('code.fail'), 'banner数量不能超过' . config('setting.banner_count') . '个');
            }
        }
        return show(config('code.success'), '修改成功');
    }

    public
    function delete($id) {
        $result = model('Article')->save(['status' => config('code.status_delete')], ['id' => $id]);
        if (!$result) {
            return show(config('code.fail'), '删除失败');
        }
        return show(config('code.success'), '删除成功');
    }

    public
    function read($id) {
        $result = model('Article')->getArticleItem($id);
        return show(config('code.success'), '获取成功', $result);
    }
}